﻿function AzureOnlineManageLeaseViewModel(context) {

    var self = this;

    var dispatcher = $({});

    var leaseId = ko.observable('');
    var action = ko.observable('');
    var container = ko.observable(null);
    var blob = ko.observable(null);

    var type = ko.observable('container');

    var leaseSettings = ko.computed(function() {
        return context.accountSettings().folder('leases');
    });
    
    var blobSettings = ko.computed(function () {
        var b = blob();
        if (b) b = b.name();
        return leaseSettings().folder('blobs').folder(b);
    });

    var containerSettings = ko.computed(function() {
        var cnt = container();
        return leaseSettings().folder('containers').folder(cnt);
    });
    
    var settings = ko.computed(function () {
        switch (type()) {
            case 'container':
                return containerSettings();
            case 'blob':
                return blobSettings();
        }
        return null;
    });

    function submitContainer() {
        var api = context.api();
        var cnt = container();
        var lid = leaseId();

        switch (action()) {
            case 'acquire':
                api.blob.leaseContainer({
                    container: cnt,
                    duration: 60,
                    action: 'acquire',
                    success: function (data, xhr) {
                        leaseId(xhr.getResponseHeader('x-ms-lease-id'));
                        dispatcher.trigger('success');
                    }
                });
                break;
            case 'release':
                api.blob.leaseContainer({
                    container: cnt,
                    action: 'release',
                    leaseId: lid,
                    success: function () {
                        dispatcher.trigger('success');
                    }
                });
                break;
            case 'break':
                api.blob.leaseContainer({
                    container: cnt,
                    action: 'break',
                    success: function () {
                        dispatcher.trigger('success');
                    }
                });
                break;
        }
    }
    
    function submitBlob() {
        var api = context.api();
        var cnt = container();
        var b = blob().name();
        var lid = leaseId();

        switch (action()) {
            case 'acquire':
                api.blob.leaseBlob({
                    container: cnt,
                    blob: b,
                    duration: 60,
                    action: 'acquire',
                    success: function (data, xhr) {
                        leaseId(xhr.getResponseHeader('x-ms-lease-id'));
                        dispatcher.trigger('success');
                    }
                });
                break;
            case 'release':
                api.blob.leaseBlob({
                    container: cnt,
                    blob: b,
                    action: 'release',
                    leaseId: lid,
                    success: function () {
                        dispatcher.trigger('success');
                    }
                });
                break;
            case 'break':
                api.blob.leaseBlob({
                    container: cnt,
                    blob: b,
                    action: 'break',
                    success: function () {
                        dispatcher.trigger('success');
                    }
                });
                break;
        }
    }
    
    function submit() {
        switch (type()) {
            case 'container':
                submitContainer();
                break;
            case 'blob':
                submitBlob();
                break;
        }
    }

    leaseId.subscribe(function () {
        settings().setItem('leaseId', leaseId() || '');
    });

    function forBlob(cnt, b) {
        container(cnt);
        blob(b);
        type('blob');
        leaseId(blobSettings().getItem('leaseId') || '');
    }
    
    function forContainer(c) {
        container(c);
        type('container');
        leaseId(containerSettings().getItem('leaseId') || '');
    }
    
    self.leaseId = leaseId;
    self.action = action;
    self.container = container;
    self.submit = submit;

    self.forBlob = forBlob;
    self.forContainer = forContainer;

    self.addOnSuccess = function (handler) {
        dispatcher.bind('success', handler);
    };

}